/*******************************************
 *Author: DennyZ
 *Data: 08/11/2014
 *E-mail: zs3292@gmail.com
 *******************************************/

#ifndef NET_SERVER_H
#define NET_SERVER_H

#include <QObject>
#include <QDateTime>
#include <QString>
#include <QStringList>
#include <QTcpServer>
#include <QUdpSocket>
#include <QDataStream>
#include "../Include/PP_Const.h"

enum UDPenum{
    MSG_ALL_USER_LIST = 1,
    MSG_CLIENT_MAC = 2,
    MSG_CLIENT_BROADCAST_INFO = 3,
    MSG_SERVER_BROADCAST_INFO = 4,
    MSG_CLIENT_CHAT = 5,
    MSG_CLIENT_BRC = 6,
    MSG_CLINET_LOGIN_SUCCESS = 7,
    MSG_CLIENT_LOGIN_FAIL = 8,
    MSG_CLINET_LOGOUT = 9
};

typedef struct {
    client_Struct client_Info;
    quint16 UDPPort;
    QString UDPIP;
}server_Struct;

int get_num_from_serList(const QString ID, QList<server_Struct> List);

class NET_SERVER : public QTcpServer
{
    Q_OBJECT
public:
    explicit NET_SERVER(QObject *parent = 0);
    bool start(QString IP, quint16 TCPPort, quint16 UDPPort);
    bool sendMsg_Udp(enum UDPenum msgTpye, QByteArray msgData = "", QHostAddress msgAds = QHostAddress::Null, quint16 port = 0);

private:
    void deMsg_UDP(QByteArray msg_UDP, QString IP, quint16 Port);
    void usersListUpdate_client();

protected:
    void incomingConnection(int socketDescriptor);
    virtual bool Init();

signals:
    void usersListUpdate(QList<server_Struct> list);
    void client_logout(QString name);
    void send_broadcast(QString msg);

private slots:
    void getClient_Struct(server_Struct client);
    void revMSG_UDP();
    void on_logout(QString name);

private:
    QUdpSocket *udpSocket;
    QList<server_Struct> clientList;
    quint16 TCPPort;
    quint16 UDPPort;

    //QHostAddress udp_IP;
    //quint16 udp_Port;
};

#endif // NET_SERVER_H
